package cn.wolfcode.trip.admin.controller;


import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 推荐游记
 */
@Controller
@RequestMapping("travelCommend")
public class travelCommendController {
    @Autowired
    private ITravelCommendService travelCommendService;

    /**
     * 列表 高查
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelCommendQueryObject qo, Model model){
        //1.根据推挤时间倒序排列
        qo.setOrderBy("tc.schedule desc");
        model.addAttribute("pageInfo",travelCommendService.query(qo));
        return "/travelCommend/list";
    }

    /**
     * 新增和编辑
     * @param travelCommend
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(TravelCommend travelCommend, MultipartFile file){
        //判断是否图片上传
        if (file != null && file.getSize()>0) {
            //上传图片到指定位置 获取路径
            String url = UploadUtil.upload(file, UploadUtil.Path + "/upload");
            //将url存到对象中
            travelCommend.setCoverUrl(url);
        }
        travelCommendService.saveOrUpdate(travelCommend);
        return new JSONResult();
    }



}
